package cn.byapps.video.vue.controller;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * ApiController
 */
@Controller
@RequestMapping("/v2/movie")
public class ApiController {

    @RequestMapping("in_theaters")
    public void inTheaters(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        /* 允许跨域的主机地址 */
        resp.setHeader("Access-Control-Allow-Origin", "*");
        /* 允许跨域的请求方法GET, POST, HEAD 等 */
        resp.setHeader("Access-Control-Allow-Methods", "*");
        /* 重新预检验跨域的缓存时间 (s) */
        resp.setHeader("Access-Control-Max-Age", "3600");
        /* 允许跨域的请求头 */
        resp.setHeader("Access-Control-Allow-Headers", "*");
        /* 是否携带cookie */
        resp.setHeader("Access-Control-Allow-Credentials", "true");

        resp.setHeader("content-type", "application/json");
        resp.setCharacterEncoding("utf-8");
        ClassPathResource classPathResource = new ClassPathResource("static/json/in_theater.json");
        InputStream inputStream = classPathResource.getInputStream();
        OutputStream sos = resp.getOutputStream();
        byte[] bytes = new byte[1024 * 8];
        int len;
        while ((len = inputStream.read(bytes)) != -1) {
            sos.write(bytes, 0, len);
        }
        inputStream.close();
        sos.flush();
        sos.close();
        
    }

    @RequestMapping("coming_soon")
    public void comingSoon(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        /* 允许跨域的主机地址 */
        resp.setHeader("Access-Control-Allow-Origin", "*");
        /* 允许跨域的请求方法GET, POST, HEAD 等 */
        resp.setHeader("Access-Control-Allow-Methods", "*");
        /* 重新预检验跨域的缓存时间 (s) */
        resp.setHeader("Access-Control-Max-Age", "3600");
        /* 允许跨域的请求头 */
        resp.setHeader("Access-Control-Allow-Headers", "*");
        /* 是否携带cookie */
        resp.setHeader("Access-Control-Allow-Credentials", "true");

        resp.setHeader("content-type", "application/json");
        resp.setCharacterEncoding("utf-8");
        ClassPathResource classPathResource = new ClassPathResource("static/json/coming_soon.json");
        InputStream inputStream = classPathResource.getInputStream();
        OutputStream sos = resp.getOutputStream();
        byte[] bytes = new byte[1024 * 8];
        int len;
        while ((len = inputStream.read(bytes)) != -1) {
            sos.write(bytes, 0, len);
        }
        inputStream.close();
        sos.flush();
        sos.close();
    }

    @RequestMapping("new_movies")
    public void newMoives(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        /* 允许跨域的主机地址 */
        resp.setHeader("Access-Control-Allow-Origin", "*");
        /* 允许跨域的请求方法GET, POST, HEAD 等 */
        resp.setHeader("Access-Control-Allow-Methods", "*");
        /* 重新预检验跨域的缓存时间 (s) */
        resp.setHeader("Access-Control-Max-Age", "3600");
        /* 允许跨域的请求头 */
        resp.setHeader("Access-Control-Allow-Headers", "*");
        /* 是否携带cookie */
        resp.setHeader("Access-Control-Allow-Credentials", "true");

        resp.setHeader("content-type", "application/json");
        resp.setCharacterEncoding("utf-8");
        ClassPathResource classPathResource = new ClassPathResource("static/json/new_movies.json");
        InputStream inputStream = classPathResource.getInputStream();
        OutputStream sos = resp.getOutputStream();
        byte[] bytes = new byte[1024 * 8];
        int len;
        while ((len = inputStream.read(bytes)) != -1) {
            sos.write(bytes, 0, len);
        }
        inputStream.close();
        sos.flush();
        sos.close();
    }

    @RequestMapping("top250")
    public void top250(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        /* 允许跨域的主机地址 */
        resp.setHeader("Access-Control-Allow-Origin", "*");
        /* 允许跨域的请求方法GET, POST, HEAD 等 */
        resp.setHeader("Access-Control-Allow-Methods", "*");
        /* 重新预检验跨域的缓存时间 (s) */
        resp.setHeader("Access-Control-Max-Age", "3600");
        /* 允许跨域的请求头 */
        resp.setHeader("Access-Control-Allow-Headers", "*");
        /* 是否携带cookie */
        resp.setHeader("Access-Control-Allow-Credentials", "true");

        resp.setHeader("content-type", "application/json");
        resp.setCharacterEncoding("utf-8");
        ClassPathResource classPathResource = new ClassPathResource("static/json/top250.json");
        InputStream inputStream = classPathResource.getInputStream();
        OutputStream sos = resp.getOutputStream();
        byte[] bytes = new byte[1024 * 8];
        int len;
        while ((len = inputStream.read(bytes)) != -1) {
            sos.write(bytes, 0, len);
        }
        inputStream.close();
        sos.flush();
        sos.close();
    }

    @RequestMapping("subject/{id}")
    public void subject(@PathVariable String id, HttpServletRequest req, HttpServletResponse resp) throws IOException {

        System.out.println(id);
        /* 允许跨域的主机地址 */
        resp.setHeader("Access-Control-Allow-Origin", "*");
        /* 允许跨域的请求方法GET, POST, HEAD 等 */
        resp.setHeader("Access-Control-Allow-Methods", "*");
        /* 重新预检验跨域的缓存时间 (s) */
        resp.setHeader("Access-Control-Max-Age", "3600");
        /* 允许跨域的请求头 */
        resp.setHeader("Access-Control-Allow-Headers", "*");
        /* 是否携带cookie */
        resp.setHeader("Access-Control-Allow-Credentials", "true");

        resp.setHeader("content-type", "application/json");

        resp.setCharacterEncoding("utf-8");
        String[] path = { "subject_6538866.json", "subject_27203005.json", "subject_27624665.json",
                "subject_30252495.json" };

        int i = new Random().nextInt(4) % path.length;
        ClassPathResource classPathResource = new ClassPathResource("static/json/"+path[i]);
        InputStream inputStream = classPathResource.getInputStream();
        OutputStream sos = resp.getOutputStream();
        byte[] bytes = new byte[1024 * 8];
        int len;
        while ((len = inputStream.read(bytes)) != -1) {
            sos.write(bytes, 0, len);
        }
        inputStream.close();
        sos.flush();
        sos.close();
    }

}